import {Switch,Route,Redirect} from 'react-router-dom';

function RouterView({routes}){
    let componentArr = routes.filter(item => item.component);
    let redirectArr = routes.filter(item => !item.component);
    return <Switch>
        {
            componentArr.map(item => <Route path={item.path} key={item.path} render={(props) =>{
                let token = localStorage.getItem('token');

                if(!token && item.path != '/login'){
                    return <Redirect  to='/login'></Redirect>
                }else{
                    return <item.component {...props} routes={item.children}></item.component>
                }
            }}></Route>)
        }
        {
            redirectArr.map(item =>  <Redirect key={item.from} from={item.from} to={item.to}></Redirect>)
        }
    </Switch>
}

export default RouterView